//
//  LongTableViewCell.m
//  GiftSay
//
//  Created by apple on 16/1/9.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "LongTableViewCell.h"
#import "HomeNewModel.h"
#import "DataBaseHandle.h"

@interface LongTableViewCell ()
@property (nonatomic, strong) UIImageView *image;
@property (nonatomic, strong) UILabel *title;
@property (nonatomic, strong) UIImageView *bg;

@property (nonatomic, strong) UIImageView *kongHeart;

@property (nonatomic, assign) BOOL ifClick;
@end

@implementation LongTableViewCell

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.ifClick = NO;
        
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
            self.backgroundColor = kNightModeColor
            ;
        } else {
            self.backgroundColor = [UIColor whiteColor];
        }
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"NightModeOpen" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"NightModeOff" object:nil];
        
        self.image = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.image .layer.masksToBounds = YES;
        self.image.userInteractionEnabled = YES;
        self.image.layer.cornerRadius = 5;
        [self addSubview:self.image];
        
        self.bg = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.bg.image = [UIImage imageNamed:@"bg.png"];
        [self.image addSubview:self.bg];
        
        self.hot = [[UIView alloc] initWithFrame:CGRectZero];
        self.hot.backgroundColor = [UIColor blackColor];
        self.hot.layer.cornerRadius = 5;
        self.hot.alpha = 0.6;
        [self.image addSubview:self.hot];
        UITapGestureRecognizer *tapHot = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHot:)];
        tapHot.numberOfTapsRequired = 1;
        tapHot.numberOfTouchesRequired = 1;
        [self.hot addGestureRecognizer:tapHot];
        
        self.kongHeart = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.kongHeart.image = [UIImage imageNamed:@"kongXin"];
        [self.hot addSubview:self.kongHeart];
        
        self.likeCount = [[UILabel alloc] initWithFrame:CGRectZero];
        self.likeCount.textColor = [UIColor whiteColor];
        self.likeCount.alpha = 1;
        self.likeCount.font = [UIFont systemFontOfSize:11 * SCREEN_WIDTH];
        self.likeCount.textAlignment = NSTextAlignmentCenter;
        [self.hot addSubview:self.likeCount];
        
        self.title = [[UILabel alloc] initWithFrame:CGRectZero];
        self.title.numberOfLines = 0;
        self.title.font = [UIFont systemFontOfSize:17 * SCREEN_WIDTH];
        self.title.textColor = [UIColor whiteColor];
        [self.image addSubview:self.title];
    }
    return self;
}

- (void)refresh {
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        self.backgroundColor = kNightModeColor
        ;
    } else {
        self.backgroundColor = [UIColor whiteColor];
    }
}

-(void)layoutSubviews {
    [super layoutSubviews];
    self.image.frame = CGRectMake(10, 10, self.width - 20, self.height - 20);
    self.bg.frame = self.image.bounds;
    self.hot.frame = CGRectMake(self.image.width / 5 * 4, -5, self.width / 10, self.width / 10 * 1);
    self.kongHeart.frame = CGRectMake(self.hot.width / 4, self.hot.width / 5, self.hot.width / 2, self.hot.width / 2);
    self.likeCount.frame = CGRectMake(0, self.hot.height - self.hot.width / 3, self.hot.width, self.hot.width / 3);
    //    定义一个字典存储label上的字体等信息
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17 * SCREEN_WIDTH]};
    //    通过字典计算高度和宽度
    CGRect rect = [self.title.text boundingRectWithSize:CGSizeMake(self.width - 40, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    self.title.frame = CGRectMake(10, self.image.height - rect.size.height - 10, self.image.width - 20, rect.size.height);
}

-(void)setModel:(HomeNewModel *)model {
    _model = model;
    
    [self.image setImageWithURL:[NSURL URLWithString:model.cover_image_url] placeholder:nil];
    self.title.text = model.title;
    
    [[DataBaseHandle shareDataBase] openDataBase];
    
    NSString *str = [NSString string];
    if (model.ids.stringValue.length == 0) {
        str = [[DataBaseHandle shareDataBase] selectMealsDressWhereId:model.url];
    }
    else{
       str = [[DataBaseHandle shareDataBase] selectMealsDressWhereId:[NSString stringWithFormat:@"%@%@",kPostInfo,model.ids]];
    }
    
//    NSLog(@"*************%@    %@",str,model.ids);
    
    NSInteger num = [model.likes_count intValue];
    if (str.length == 0 || [str isEqual:nil]) {
        self.kongHeart.image = [UIImage imageNamed:@"kongXin"];
        self.ifClick = NO;
    }
    
    else{
        self.kongHeart.image = [UIImage imageNamed:@"shiXin"];
        self.kongHeart.frame = CGRectMake(self.kongHeart.centerX, self.kongHeart.centerY, 0, 0);
        self.ifClick = YES;
        num += 1;
    }
    
    self.likeCount.text = [NSString stringWithFormat:@"%ld", num];
    
}

#pragma mark -- tap
-(void)tapHot:(UITapGestureRecognizer *)tap {
    NSString *count = [NSString stringWithFormat:@"%@", self.likeCount.text];
    NSInteger num = [count intValue];
    if (self.ifClick == NO) {
        self.kongHeart.image = [UIImage imageNamed:@"shiXin"];
        self.kongHeart.frame = CGRectMake(self.kongHeart.centerX, self.kongHeart.centerY, 0, 0);
        [UIView animateWithDuration:0.3 animations:^{
            [UIView setAnimationDelay:0];
            self.kongHeart.frame = CGRectMake(self.hot.width / 4, self.hot.width / 5, self.hot.width / 2, self.hot.width / 2);
        }];
        num += 1;
        self.ifClick = YES;
        
        [[DataBaseHandle shareDataBase] insertMealsID:[NSString stringWithFormat:@"%@%@",kPostInfo,self.model.ids] AndName:self.model.title AndType:@"post" AndImg:self.model.cover_image_url];
        
    } else {
        self.kongHeart.image = [UIImage imageNamed:@"kongXin"];
        num -= 1;
        self.ifClick = NO;
        [[DataBaseHandle shareDataBase] deleteMealsWhereID:self.model.url];
        [self.delegate deleteing:self.model.title];
    }
    self.likeCount.text = [NSString stringWithFormat:@"%ld", num];
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
